<?php

class Control_Model_Table_SubPage extends Zend_Db_Table_Abstract {

    protected $_name = 'dynamic_pages';

    public function getAllPages() {
        $select = $this->select();
        $select->order('id DESC');
        $subPage = $this->_db->query($select)->fetchAll();
        if ($subPage != array()) {
            return $subPage;
        } else {
//        Zend_Debug::dump($content['0']['block_first']);exit();
        }
    }

    public function getPage($id) {
        $id = (int) $id;
        $select = $this->select();
        $select->order('id DESC')
                ->where('id=?', $id);
        $subPage = $this->_db->query($select)->fetchAll();
        if ($subPage != array()) {
            $content = new Default_Model_Table_SubPageContent();
            $contentPage['1'] = $content->getPageContent($subPage['0']['id']);
            $contentCount = (int) count($content);
            $content = $subPage + $contentPage;
            return $content;
        } else {
//        Zend_Debug::dump($content['0']['block_first']);exit();
        }
    }

    public function getPagesToMenu() {
        $select = $this->select();
        $select->from($this->_name, array('id', 'name', 'showin_about',
                    'showin_services', 'showin_news'))
                ->order('id DESC');
        $subPage = $this->_db->query($select)->fetchAll();
        return $subPage;
    }

    public function getPagesToMenuWhere($mode) {
        $select = $this->select();
        if ($mode == 1) {
            $select->from($this->_name, array('id', 'name', 'showin_about',
                        'showin_services', 'showin_news'))
                    ->order('id DESC')
                    ->where('showin_about=?','1');
        } elseif ($mode == 2) {
            $select->from($this->_name, array('id', 'name', 'showin_about',
                        'showin_services', 'showin_news'))
                    ->order('id DESC')
                    ->where('showin_services=?','1');
        } elseif ($mode == 3) {
            $select->from($this->_name, array('id', 'name', 'showin_about',
                        'showin_services', 'showin_news'))
                    ->order('id DESC')
                    ->where('showin_news=?','1');
        }
        $subPage = $this->_db->query($select)->fetchAll();
        return $subPage;
    }

    public function insertPage($title, $name, $about_page, $meta_desc, $meta_keyw, $block_first, $foreach_first, $foreach_last, $block_second, $showin_about, $showin_services, $showin_news) {
        $data = array(
            'title' => $title,
            'name' => $name,
            'about_page' => $about_page,
            'meta_desc' => $meta_desc,
            'meta_keyw' => $meta_keyw,
            'block_first' => $block_first,
            'foreach_first' => $foreach_first,
            'foreach_last' => $foreach_last,
            'block_second' => $block_second,
            'showin_about' => $showin_about,
            'showin_services' => $showin_services,
            'showin_news' => $showin_news
        );
        $this->insert($data);
    }

    public function updatePage($id, $title, $name, $about_page, $meta_desc, $meta_keyw, $block_first, $foreach_first, $foreach_last, $block_second, $showin_about, $showin_services, $showin_news) {
        $data = array(
            'title' => $title,
            'name' => $name,
            'about_page' => $about_page,
            'meta_desc' => $meta_desc,
            'meta_keyw' => $meta_keyw,
            'block_first' => $block_first,
            'foreach_first' => $foreach_first,
            'foreach_last' => $foreach_last,
            'block_second' => $block_second,
            'showin_about' => $showin_about,
            'showin_services' => $showin_services,
            'showin_news' => $showin_news
        );
        $this->update($data, 'id = ' . (int) $id);
    }

    public function deletePage($id) {
        $this->delete('id =' . (int) $id);
    }

}

